package com.yiren.manager.ctrl.pension;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrMaterial;
import com.yiren.entity.YrMaterialApply;
import com.yiren.entity.YrStaffInfo;
import com.yiren.entity.YrSubjectRoom;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.department.YrSubjectRoomForm;
import com.yiren.manager.form.pension.YrMaterialApplyForm;
import com.yiren.manager.form.pension.YrMaterialForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.department.YrSubjectRoomService;
import com.yiren.manager.service.iface.pension.YrMaterialApplyService;
import com.yiren.manager.service.iface.pension.YrMaterialService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("materialApply")
@Controller
public class YrMaterialApplyController extends GenericController<YrMaterialApplyForm, YrMaterialApply>{

	@Autowired
	private YrMaterialApplyService selfSerivce;
	
	@Autowired
	private YrMaterialService materialService;
	
	@Autowired
	private YrSubjectRoomService subjectRoomService ;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrMaterialApplyForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrMaterialApplyForm> result = new HTTPResult<YrMaterialApplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrMaterialApplyForm form = tool.mapper("materialApply", YrMaterialApplyForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getMaterialApplyId()||form.getMaterialApplyId().isEmpty()){
			form.setMaterialApplyId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
			form.setGrantStatus(0);
			form.setApplyDate(new Date());
		}
		form.setUpdateTime(new Date());
		YrMaterialApply entity = ObjectUtils.objectToObject(form, YrMaterialApply.class);
		selfSerivce.save(entity);
		
		return result;
	}
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrMaterialApplyForm> find(String subjectRoomId) throws Exception{
		HTTPResult<YrMaterialApplyForm> result = new HTTPResult<YrMaterialApplyForm>();
		String sqlWhere = "subject_room_id=?1";
		QueryResult<YrMaterialApply> qr = selfSerivce.find(YrMaterialApply.class, sqlWhere, new Object[]{subjectRoomId});
		List<YrMaterialApplyForm> formList = new ArrayList<YrMaterialApplyForm>();
		for(YrMaterialApply entity : qr.getResultList()){
			YrMaterialApplyForm form = toForm(entity);
			YrMaterial material = materialService.findOne(form.getMaterialId());
			YrSubjectRoom subjectRoom = subjectRoomService.findOne(form.getSubjectRoomId());
			YrStaffInfo staffInfo = staffInfoService.findOne(form.getCreateId());
			if(null!=material){
				form.setMaterialForm(ObjectUtils.objectToObject(material, YrMaterialForm.class));
			}
			if(null!=subjectRoom){
				form.setSubjectRoomForm(ObjectUtils.objectToObject(subjectRoom, YrSubjectRoomForm.class));
			}
			if(null!=staffInfo){
				form.setHandler(staffInfo.getName());
			}
			formList.add(form);
		}
		result.setData(formList);
		
		return result;
	}
	
	
	/**
	 * 材料发放
	 * @param data
	 * @return
	 * @throws SwallowException 
	 * @throws MyException 
	 */
	@RequestMapping("grantOne")
	@ResponseBody
	public HTTPResult<YrMaterialApplyForm> grantOne(String id) throws MyException{
		HTTPResult<YrMaterialApplyForm> result = new HTTPResult<YrMaterialApplyForm>();
		if(null==id){
			throwExp("参数有误");
		}
		YrMaterialApply entity = selfSerivce.findOne(id);
			if(null!=entity){
				entity.setGrantStatus(1);
				selfSerivce.save(entity);
			}
		return result;
	}
	
	@RequestMapping("deleteOne")
	@ResponseBody
	public HTTPResult<YrMaterialApplyForm> deleteOne(String id) throws SwallowException, MyException{
		HTTPResult<YrMaterialApplyForm> result = new HTTPResult<YrMaterialApplyForm>();
		if(null==id){
			throwExp("参数有误");
		}
		selfSerivce.delete(id);
		return result;
	}
	
	/**
	 * 材料发放
	 * @param data
	 * @return
	 * @throws SwallowException 
	 * @throws MyException 
	 */
	@RequestMapping("grant")
	@ResponseBody
	public HTTPResult<YrMaterialApplyForm> grant(String data) throws SwallowException, MyException{
		HTTPResult<YrMaterialApplyForm> result = new HTTPResult<YrMaterialApplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		for(String id : ids){
			YrMaterialApply entity = selfSerivce.findOne(id);
			if(null!=entity){
				entity.setGrantStatus(1);
				selfSerivce.save(entity);
			}
		}
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrMaterialApplyForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrMaterialApplyForm> result = new HTTPResult<YrMaterialApplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			selfSerivce.delete(id);
		}
		return result;
	}
	
	@Override
	public YrMaterialApplyForm toForm(YrMaterialApply e) throws Exception {
		YrMaterialApplyForm form = ObjectUtils.objectToObject(e, YrMaterialApplyForm.class);
		return form;
	}

	@Override
	public YrMaterialApply toEntity(YrMaterialApplyForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
